package com.wjl.picture.wx.handler;

import cn.hutool.core.util.IdUtil;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpMessageHandler;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class EventHandler implements WxMpMessageHandler {

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMpXmlMessage, Map<String, Object> map, WxMpService wxMpService,
                                    WxSessionManager wxSessionManager) throws WxErrorException {
        final String content = "您点击了菜单";
        // 生成一个雪花算法的 ID（长整型）
        long id = IdUtil.getSnowflake().nextId();

        // 获取 ID 的最后 6 位数字
        String sixDigitId = String.valueOf(id).substring(String.valueOf(id).length() - 6);
        // 调用接口，返回验证码
        return WxMpXmlOutMessage.TEXT().content(sixDigitId)
                .fromUser(wxMpXmlMessage.getToUser())
                .toUser(wxMpXmlMessage.getFromUser())
                .build();
    }
}
